/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.apps.controllers;

import com.apps.beans.AccountBean;
import com.apps.ui.SearchResultScreen;
import com.apps.utils.SearchHelper;
import com.apps.utils.StartupHelper;
import java.awt.Desktop;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map;

/**
 *
 * @author ARIJIT PAL
 */
public class SearchController{

    public static void search(String inputText, String localDir, SearchResultScreen searchUI){
        Map<String, ShellController> shellMap = StartupHelper.getShellMap();
        ArrayList<AccountBean> accList = StartupHelper.getAccList(StartupHelper.getSettings().getApps_env_code());
        System.err.println("AccList: "+accList+"env Code: "+StartupHelper.getSettings().getApps_env_code());
        for(String accID : shellMap.keySet()){
            //check for the accID in the list
            for(AccountBean acc : accList){
                if(acc.getCode().equalsIgnoreCase(accID)){
                    ShellController shell = shellMap.get(accID);
                    if(shell != null && shell.getShellStatus() && shell.getActiveFlag()){
                        searchUI.setSearchCount(searchUI.getSearchCount()+1);
                        //Start: open search thread
                        search(searchUI, localDir, shell, acc, inputText, 45000);//timeout:45sec
                        //End: open search thread
                    }else{
                        System.err.println(accID + "has not beed found/the shell is dead");
                    }
                    //exit the loop, because one match has been found in the AccList
                    break;
                }
            }
        }
    }
    
    public static void search(SearchResultScreen searchUI, String localDir, ShellController shell, AccountBean account, String searchText, int timeOut)
    {
        SearchHelper searchHelper = new SearchHelper(localDir, shell, account, searchText, timeOut);
        searchHelper.setUI(searchUI);
        Thread th = new Thread(searchHelper);
        th.start();
    }
    public static String createResultDir(AccountBean account, String searchDir){
        String dir = account.getHost() + "."
                + Calendar.getInstance().getTimeInMillis() + String.valueOf((int) Math.random() * 10);
        StartupHelper.loadSettings();
        //String resultPath = StartupHelper.getSettings().getApps_result_path();
        //System.out.println(resultPath);
        dir = (searchDir==null?"":(searchDir+"/")) + dir;
        //System.out.println(dir);
        boolean success = dir.contains("/")?(new File(dir)).mkdirs():(new File(dir)).mkdir();
        if (success) {
            return dir;
        }else
            return null;
    }
    public static void openResultDir(String dir) throws Exception{
        System.out.println(dir);
        //Runtime.getRuntime().exec("explorer /select,  " + dir);
        Desktop.getDesktop().open(new File(dir));
    }
    
    public static void main(String[] args) throws Exception{
        AccountBean ab = new AccountBean();
                ab.setHost("some.net");
        String dir = createResultDir(ab,"Result/Search.12345");
        openResultDir(dir);
        System.out.println("END");
    }
    
}
